# -*- coding:utf-8 -*-
#处理商品的数据
import requests
import json
from pymongo import MongoClient
from product.mongodb import mongodb


conn = MongoClient(mongodb.ip, 27017)
db = conn.pinduoduo
goodsReviewsDb = db.goodsReviews
headers = {
    'User-Agent':'android Mozilla/5.0 (Linux; Android 8.1.0; vivo NEX A Build/OPM1.171019.019; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36  phh_android_version/3.23.0 phh_android_build/228842 phh_android_channel/qihu360',
    'accesstoken':'XDSAH2KVTASDV6UOZFTSLKNRT5K4K6TOQFADU4YTV3CMGQ6GY3BQ1007e2d'
}
urls = 'http://apiv4.yangkeduo.com/reviews/{0}/list?size={1}&page={2}'
ip={
    'http':'http://183.129.207.80:21231',
    'https':'https://119.122.213.14:9000'
}

class reviews:
    def getReviewsData(self,goodsId,size,page):
        data = requests.get(url=urls.format(goodsId,size,page),headers=headers).json()
        print(data)
        #如果json存在数据，存入数据
        if("data" in data):
            if('' == data['data']):
                return False
            for key in data['data']:
                #遍历款式
                #specsJson = json.loads(data['data'])
                specsStr = []
                for specsKey in json.loads(key['specs']):
                    specsStr.append("{0}|{1}".format(str(specsKey['spec_key']),str(specsKey['spec_value'])))
                append = ''
                if('append' in key):
                    append = key['append']
                goodsReviewsDb.insert({
                    'uid':key['uid'],
                    'goods_id':goodsId,
                    'comment':key['comment'],
                    'stars':key['stars'],
                    'desc_score':key['desc_score'],
                    'logistics_score':key['logistics_score'],
                    'service_score':key['service_score'],
                    'specs':','.join(specsStr),
                    'time':key['time'],
                    'avatar':key['avatar'],
                    'name':key['name'],
                    'pictures':key['pictures'],
                    'append':append,
                    'review_id':key['review_id']
                })
        return True